<?php
declare (strict_types=1);

namespace app\common\model;

class UserSign extends Base
{
    public static function add($user): array
    {
        parent::startTrans();
        try {
            if ($user->is_day_sign == 1) {
                throw new \Exception('您今天已经签到过了！');
            }
            parent::create([
                'uid' => $user->id,
                'create_date' => date('Y-m-d')
            ]);
            $user->sign_day = $user->sign_day + 1;
            $user->always_day = $user->always_day + 1;;
            $user->is_day_sign = 1;
            $user->save();

            parent::commit();
        } catch (\Exception $e) {
            parent::rollback();

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '签到成功', 'always_day' => $user->always_day, 'sign_day' => $user->sign_day];
    }
}